package com.zxy.fmmall.service.impl;

import com.zxy.fmmall.dao.ProductCommentsMapper;
import com.zxy.fmmall.entity.ProductComments;
import com.zxy.fmmall.entity.ProductCommentsVo;
import com.zxy.fmmall.service.ProductCommentService;
import com.zxy.fmmall.utils.PageHelper;
import com.zxy.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

@Service
public class ProductCommentServiceImpl implements ProductCommentService {

    @Autowired
    private ProductCommentsMapper productCommentsMapper;
    @Override
    public ResultVo selectProductComment(String productId,int pageNum,int  limit) {

        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        int count = productCommentsMapper.selectCountByExample(example);
        int start = (pageNum-1)*limit;
        int PageCount = count%limit==0? count/limit : count/limit +1;

        PageHelper<ProductCommentsVo> productCommentsVos = new PageHelper<>();
        List<ProductCommentsVo> list = productCommentsMapper.selectProductComment(productId,start,limit);
        productCommentsVos.setList(list);
        productCommentsVos.setCount(count);
        productCommentsVos.setPageCount(PageCount);
        return ResultVo.ok().data("productsComment",productCommentsVos);
    }

    @Override
    public ResultVo getProductComment(String pid) {

        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        int count = productCommentsMapper.selectCountByExample(example);


        criteria.andEqualTo("commLevel",1);

        int goodComont = productCommentsMapper.selectCountByExample(example);

        Example example1 = new Example(ProductComments.class);
        Example.Criteria criteria1= example1.createCriteria();
        criteria1.andEqualTo("commLevel",2);
        int midComont = productCommentsMapper.selectCountByExample(example1);

        Example example2 = new Example(ProductComments.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("commLevel",3);
        int badComont = productCommentsMapper.selectCountByExample(example2);

        Double percents = (Double.parseDouble(goodComont+"")/Double.parseDouble(count+""))*100;
        String percent = (percents+"").substring(0,(percents.toString().lastIndexOf(".")+3));
        HashMap<String, Object> commontsCountMap = new HashMap<>();
        commontsCountMap.put("total",count);
        commontsCountMap.put("percent",percent);
        commontsCountMap.put("goodComont",goodComont);
        commontsCountMap.put("midComont",midComont);
        commontsCountMap.put("badComont",badComont);
        return ResultVo.ok().data("commontsCountMap",commontsCountMap);
    }
}
